package br.senac.arqpad.pizzaria.controller;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.senac.arqpad.pizzaria.model.IngredienteFacade;

/**
 * Servlet implementation class PizzariaIncluir
 */
@WebServlet("/IngredienteExcluir")
public class IngredienteExcluir extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public IngredienteExcluir() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		String urlProximaPagina = "ingredientes.jsp";
		String mensagemApresentarNaProximaPagina = "";
		String acao = "";
		
		try {

		IngredienteFacade facade = new IngredienteFacade();

		if ( facade.excluir( Long.parseLong( request.getParameter("id") )) ) {		 
			mensagemApresentarNaProximaPagina="Ingrediente excluido com sucesso";
		} else { mensagemApresentarNaProximaPagina="Ingrediente Nao excluido"; }
		
		request.setAttribute("lista", facade.listar() );	
		
		}
		catch (Exception e) {
			mensagemApresentarNaProximaPagina = "Erro ao " + acao + "<br/>Detalhes:<br/>" + e.getMessage();
			urlProximaPagina = "erro.jsp";
		}

		request.setAttribute("mensagem",mensagemApresentarNaProximaPagina);
		RequestDispatcher rd = request.getRequestDispatcher(urlProximaPagina);

		try
		{
			rd.forward(request,response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			mensagemApresentarNaProximaPagina = "Erro ao " + acao + "<br/>Detalhes:<br/>" + e.getMessage();
			urlProximaPagina = "erro.jsp";
			//response.sendRedirect("estadocivil.jsp?mensagem=Erro ao " + acao);
		}
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
